package com.bwie.parkdemo.mvvm.viewmodel;

import android.app.Application;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.View;

import com.bwie.lib_core.action.ActActionField;
import com.bwie.lib_core.base.BaseEntity;
import com.bwie.lib_core.bind.rvadapter.GridVerticalAdapterEntity;
import com.bwie.lib_core.mvvm.base.BaseViewModel;
import com.bwie.parkdemo.R;
import com.bwie.parkdemo.adapter.ParkingAllAdapter;
import com.bwie.parkdemo.mvvm.model.ParkingAllModel;
import com.bwie.parkdemo.mvvm.model.entity.ParkingAllEntity;
import com.bwie.parkdemo.mvvm.ui.activity.ApplyParkingActivity;
import com.bwie.parkdemo.widget.dividerdtemdecoration.ParkDividerItemDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yanyusong.y_divideritemdecoration.Y_DividerItemDecoration;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 全部车位的ViewModel
 * 按钮监听器;全部车位接口请求；万能适配器设置添加
 * */
public class ParkingAllViewModel extends BaseViewModel<ParkingAllModel> {

    public GridVerticalAdapterEntity gridVerticalAdapterEntity;
    public int divider = ParkDividerItemDecoration.ID;//指定使用那个分隔线
    public ParkingItemListener itemListener;
    public boolean resultFlag = false;

    public ParkingAllViewModel(@NonNull Application application) {
        super(application);
        //声明recyclerview item点击事件
        itemListener = new ParkingItemListener();
        //创建与RecyclerView关联的适配器以及布局管理器
        gridVerticalAdapterEntity = new GridVerticalAdapterEntity();
        gridVerticalAdapterEntity.setSpanCount(6);
        gridVerticalAdapterEntity.setDatas(new ArrayList<>());
        gridVerticalAdapterEntity.setOrientation(GridLayoutManager.VERTICAL);
        gridVerticalAdapterEntity.setType(0);
        gridVerticalAdapterEntity.setLayoutId(R.layout.item_parkingall);
    }

    @Override
    protected ParkingAllModel createModel() {
        return new ParkingAllModel();
    }

    @Override
    protected void initData() {
        //获取全部车位数据
        m.request(null).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                        showDialog();
                    }

                    @Override
                    public void onNext(BaseEntity entity) {
                        //网络请求成功->向下设置适配器最新数据
                        if (entity.statuesCode.equals("200")) {
                            ParkingAllEntity parkingAllEntity = (ParkingAllEntity) entity;
                            ArrayList<Object>list = parkingAllEntity.getParkinges();
                            createLeft(list);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("ZXY","onNext:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        disDialog();//关闭dialog
                    }
                });
    }

    //添加左侧列表的引导列
    private void createLeft(ArrayList<Object>list){
        ArrayList<Object>allList = new ArrayList<>();
        allList.addAll(list);
        int line;
        int index = list.size()%5;
        if (index == 0)
            line = list.size()/5;
        else
            line = (list.size() - index)/5+1;
        for (int i = 0;i < line;i ++){
            ParkingAllEntity.Values values = new ParkingAllEntity.Values();
            values.leftFlag = true;
            values.parkName = "第"+(i+1)+"行";
            int k = i*6;
            Log.e("ZXWQ","K:"+k);
            allList.add(k,values);
        }
        gridVerticalAdapterEntity.setDatas(allList);
    }

    public class ParkingItemListener implements BaseQuickAdapter.OnItemClickListener{

        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            ParkingAllEntity.Values entity = (ParkingAllEntity.Values) adapter.getItem(position);
            if (!resultFlag) {
                Map<String, Object> map = new HashMap<>();
                map.put(ActActionField.ACTCLAZZ, ApplyParkingActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt(ActActionField.ACTBUNDLE, entity.parkId);
                bundle.putString("parkName", entity.parkName);
                map.put(ActActionField.ACTBUNDLE, bundle);
                startAct(map);
            }else {
                EventBus.getDefault().post(entity);
            }
        }
    }

    //车位申请完成刷新当前方法
    public void refreshParkingAll(){
        initData();
    }
    //全部车位界面监听器
    /*
     * 1.返回按钮需要监听;
     * 2.提交按钮监听;
     * 3.添加车位信息界面;
     * 4.我的车辆信息;
     * */
    public class ParkingBtnListener{
        public void backListener(){
            finishAct();
        }
        //提交
        public void sendParking(){

        }
        //添加车位信息
        public void addCar(){

        }
        //我的车辆信息
        public void carValues(){
            //跳转到车辆信息详情页
        }

    }

}
